package v1

import (
	"gin_mall_tmp/pkg/utils"
	"gin_mall_tmp/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

func ListFavourite(c *gin.Context) {
	claim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	listFavouriteService := service.FavouriteService{}
	if err := c.ShouldBind(&listFavouriteService); err == nil {
		res := listFavouriteService.List(c.Request.Context(), claim.ID)
		c.JSON(http.StatusOK, res)
	} else {
		c.JSON(http.StatusBadRequest, ErrorResponse(err))
		utils.LogrusObj.Infoln(err)
	}
}

func CreateFavourite(c *gin.Context) {
	claim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	createFavouriteService := service.FavouriteService{}
	if err := c.ShouldBind(&createFavouriteService); err == nil {
		res := createFavouriteService.Create(c.Request.Context(), claim.ID)
		c.JSON(http.StatusOK, res)
	} else {
		c.JSON(http.StatusBadRequest, ErrorResponse(err))
		utils.LogrusObj.Infoln(err)
	}
}

func DeleteFavourite(c *gin.Context) {
	claim, _ := utils.ParseToken(c.GetHeader("Authorization"))
	deleteFavouriteService := service.FavouriteService{}
	if err := c.ShouldBind(&deleteFavouriteService); err == nil {
		res := deleteFavouriteService.Delete(c.Request.Context(), claim.ID, c.Param("id"))
		c.JSON(http.StatusOK, res)
	} else {
		c.JSON(http.StatusBadRequest, ErrorResponse(err))
		utils.LogrusObj.Infoln(err)
	}
}
